﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.DataAccess.Repository;
using LY.Entity.Entities;
using LY.Interface;
using LY.Common;
using LY.Entity;
using System.Transactions;
using LY.Common.Utils;
using Newtonsoft.Json;

namespace LY.Business.ServiceImpl
{
    public class LoginService : ILoginService
    {
        LoginReponsitory lReponsitory;
        ScoreRepository _srepo;

        public LoginService()
        {
            lReponsitory = new LoginReponsitory();
            _srepo = new ScoreRepository();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <param name="no"></param>
        /// <returns></returns>
        public Member_EX LoginUser(string username, string password) 
        {
            return lReponsitory.ValidateLogOn(username, password);
        
        }
        //根据ID获取会员信息
        public Member_EX GetUserById(int id)
        {
            return lReponsitory.GetUserById(id);
        }
        //会员注册账号保存
        public int RegisterSave(U_Member member)
        {
            int ret=0;
            using (TransactionScope scope = new TransactionScope())
            {
                //会员保存
                //GetCityByIP("112.65.163.22");
                ret=(int)lReponsitory.Insert(member);
                //添加积分
                U_Score tscore = new U_Score();
                tscore.MemberId = member.ID;
                tscore.InCome = 50;
                tscore.OutPay = 0;
                tscore.Balance = 0;
                tscore.CreateTime = DateTime.Now;
                //tscore.CreateUser = investment.CreateUser;
                //tscore.ProjectId = investment.ProjectId;
                tscore.Type = 1;//注册获得积分
                _srepo.InsertScore(tscore);
                //设置会员账号
                U_Account account = new U_Account()
                {
                    MemberId = ret,
                    AccountName=member.Mobilenumber,
                    Status=1,
                    CreateTime=DateTime.Now

                };
                lReponsitory.Insert(account);
                scope.Complete();
            }
            return ret;
        }
        
        //验证用户名是否重
        public int RegisterValite(string name)
        {
            name = Utility.ClearSafeStringParma(name);
            return lReponsitory.RegisterValite(name);
        }

        //验证手机号是否重
        public int MobileValite(string mobile)
        {
            mobile = Utility.ClearSafeStringParma(mobile);
            return lReponsitory.MobileValite(mobile);
        }
        //手机验证码是否有效
        public int MobileCodeValite(string mobile, string code, int codeId)
        {
            mobile = Utility.ClearSafeStringParma(mobile);
            return lReponsitory.MobileCodeValite(mobile,code,codeId);
        }

        //保存验证码
        public int CodeSave(int code,string tel,int type)
        {
            
           /* var Code = new object{Code = code, Tel = tel, Type = type}; 

            return (int)lReponsitory.Insert(Code);*/
            return 1;
        }

        //验证用户账号和手机号 是否正确 
        public int MemberMobileValite(string name, string mobile)
        {
            name = Utility.ClearSafeStringParma(name);
            mobile = Utility.ClearSafeStringParma(mobile);
            return lReponsitory.MemberMobileValite(name, mobile);
        }
        /// <summary>
        /// 根据IP获取省份城市
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public string[] GetCityByIP(string ip)
        {
            string[] retArray = null;
            string url = "http://api.map.baidu.com/location/ip?ak=2t1thADXOLGBhtkDdIhWUcbd&ip="+ip+"&coor=bd09ll";
            string ret=Net.HttpRequest(url, "", "GET", 2000, Encoding.Default);

            Location_EX location = JsonConvert.DeserializeObject<Location_EX>(ret);
            if (location.address != null)
            {
                retArray = location.address.Split('|');
            }
            return retArray;

        }

        //重置密码
        public int ResetSave(U_Member member)
        {
            return lReponsitory.ResetSave(member);
        }
    }
}
